/*
 * Copyright (C) 2009 Kristopher T Babic
 *
 * This program is free software; you can redistribute it and/or modify it under the terms of
 * the GNU General Public License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses>.
 */

package edu.idp.client.advrubberband;

import edu.idp.client.interfaces.Sizeable;
import edu.idp.shared.StoredObject;
import edu.idp.shared.utils.Convert;

import java.awt.*;

/**
 * A Rubberband that does lines.
 *
 * @author Kristopher T Babic
 */
public class RubberbandLine extends Rubberband implements Sizeable { //implements Storable {
    private int size = 1;

    public RubberbandLine() {
    }

    public RubberbandLine(Component component) {
        super(component);
    }

    public void drawEnd(Graphics graphics) {

        if (graphics == null)
            return;

        Graphics2D g = (Graphics2D) graphics;

        g.setStroke(new BasicStroke((size > 0) ? size : 0, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER));
        g.setColor(getColor());
        g.drawLine(anchorPt.x, anchorPt.y, endPt.x, endPt.y);

    }

    public void drawLast(Graphics graphics) {

        if (graphics == null)
            return;

        graphics.drawLine(anchorPt.x, anchorPt.y,
                          lastPt.x, lastPt.y);
    }

    public void drawNext(Graphics graphics) {

        if (graphics == null)
            return;

        graphics.drawLine(anchorPt.x, anchorPt.y,
                          stretchedPt.x, stretchedPt.y);
    }

    public int getType() {
        return StoredObject.LINE;
    } //DrawType.LINE; }

    protected String storeExtra() {
        return Convert.fromInt(this.size);
    }

    protected void restoreExtra(String S) {
        if (S == null || S.length() == 0)
            size = 1;
        else
            size = Convert.toInt(S);
    }

    public void setSize(int size) {
        this.size = size;
    }

    public int getSize() {
        return this.size;
    }

}
